﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AllInOne.Entity;
using PropertyChanged;

using MessageBox = HandyControl.Controls.MessageBox;

namespace AllInOne.MainPages
{
    /// <summary>
    /// Interaction logic for HistoryEdit.xaml
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class PlateInPut : Page
    {
        public Record RecordModel { get; set; }

        //public List<MaterialModel> Materials { get; set; }
        public int MaterialSelectIndex { get; set; }

        public event DlgDialog DlgDialogEvent_InputPlate;

        public List<ProvinceAbbModel> Provinces { get; set; }
        public int ProvinceSelectIndex { get; set; } = 0;

        public int UserSelectIndex { get; set; }



        private MainWindow parent;

        public PlateInPut(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }
        public void InitPage()
        {
            txtCarNumber.Text="KC0925";
            //if (this.parent.businessInfo.sjhm != null)
            //{
            //    this.txtSjhm.Text = this.parent.businessInfo.sjhm;
            //}
            //else
            //{
            //    this.txtSjhm.Text = this.parent.drivingLicence.sjhm;
            //}
            //this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //加载省份简称
            Task.Factory.StartNew(() =>
            {
                List<ProvinceAbbModel> TempList = new List<ProvinceAbbModel>();
                for (int i = 0; i < 41; i++)
                {
                    ProvinceAbbModel temp = new ProvinceAbbModel();
                    temp.Id = i;
                    temp.ProvinceAbbName = ((MyConst.PlateProvince)i).ToString();
                    temp.Describe = ((MyConst.PlateProvince)i).ToString();
                    TempList.Add(temp);
                }
                Provinces = TempList;

                if (Provinces != null && Provinces.Count > 0)
                {
                    var index2 = Provinces.FindIndex(x => x.ProvinceAbbName.Equals("蒙"));
                    if (index2 != -1) ProvinceSelectIndex = index2;
                }
            });
        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //关闭页面，返回到上一级页面
            //this.Close();
            this.parent.ResetIndex();
        }

        private void setBackgroundImage(object sender, string url)
        {
            TextBlock btn = (TextBlock)sender;
            btn.Background = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri(url)),
                Stretch = Stretch.Uniform
            };
        }

        private void BigBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/big_btn.png");
        }

        private void BigBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/big_btn_hover.png");
        }

        private void BigBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/big_btn.png");
            TextBlock btn = (TextBlock)sender;
            //this.txtPrefix.Text = btn.Text;
        }

        private void SmallBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn.png");

        }

        private void SmallBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn_hover.png");
        }

        private void SmallBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn.png");

            TextBlock btn = (TextBlock)sender;
            string carNumber = this.txtCarNumber.Text;
            if (carNumber.Length < 6 || (carNumber.Length < 7 && (carNumber.Substring(1, 1) == "D" || carNumber.Substring(1, 1) == "F")))
            {
                this.txtCarNumber.Text = carNumber + btn.Text;
            }
        }

        private void DeleteBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn.png");

        }

        private void DeleteBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn_hover.png");
        }

        private void DeleteBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn.png");
            if (this.txtCarNumber.Text.Length > 0)
            {
                string carNumber = this.txtCarNumber.Text;
                this.txtCarNumber.Text = carNumber.Substring(0, carNumber.Length - 1);
            }
        }

        private void ClearBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn.png");

        }

        private void ClearBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn_hover.png");

        }

        private void ClearBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn.png");
            this.txtCarNumber.Text = "";
        }

        private void BtnSubmit_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (this.verifyCarNumber())
            {
                string carNumber = Provinces[ProvinceSelectIndex].ProvinceAbbName + this.txtCarNumber.Text;
                //if (DlgDialogEvent_InputPlate != null)
                //{
                //    DlgDialogEvent_InputPlate(carNumber);
                //}
                ////Close();

                //this.parent.ResetIndex();
                //回到上一级菜单页面
                this.parent.GiveCard_Page(carNumber);
            }
            else { MessageBox.Show("提示", "您输入的号牌号码不正确，请检查您的输入。"); }
        }

        /// <summary>
        /// 校验输入的车牌是否正确
        /// </summary>
        /// <returns></returns>
        private bool verifyCarNumber()
        {
            string carNumber = this.txtCarNumber.Text;
            if (carNumber.Length >= 6)
            {
                return true;
            }
            return false;
        }
    }
}
